package edu.asu.sefcom.extract.profile.data;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class TranslateUtils {

	
	public static Date getDate(String dateString){
		
		int dfltYear=0,dfltDay=0;
		String dfltMonth =null;
		Date date = null;
		
		if(dateString!=null&&(dateString.length()>10)){
			//search Year
				Pattern pattern = Pattern.compile("\\d{4}");
				Matcher matcher = pattern.matcher(dateString);
				
				try{
					if(matcher.find())
						dfltYear= Integer.parseInt(matcher.group().trim());
					else{
						System.out.println("ass date format");
						pattern = Pattern.compile("\\d{2}\\s");
						matcher = pattern.matcher(dateString.toLowerCase());
						if(matcher.find()){
							System.out.println("Year @@:" +matcher.group());
							dfltYear = Integer.parseInt(matcher.group().trim());
							System.out.println("dfltYear");
							dateString = dateString.substring(0,matcher.start()-1);
							System.out.println("After  Year :"+ dateString);
						}
						pattern = Pattern.compile("/\\d{2}");
						matcher = pattern.matcher(dateString.toLowerCase());
						if(matcher.find()){
							dfltDay = Integer.parseInt(dateString.substring(matcher.start()+1, matcher.end()));
							dateString = dateString.substring(0,matcher.start());
							System.out.println("Day :" +matcher.group() );
							System.out.println("Remaing :" +dateString );
						}
						pattern = Pattern.compile("\\d+");
						matcher = pattern.matcher(dateString.toLowerCase());
						if(matcher.find()){
							dfltMonth =(matcher.group());
							System.out.println("Month :" +matcher.group() );
						}
						if(dfltMonth.length()!=2)
							dfltMonth = "0"+dfltMonth;
						String str = dfltDay+"/"+dfltMonth+"/200"+dfltYear;
						System.out.println("str"+ str);
						SimpleDateFormat formatter = new SimpleDateFormat("dd/MM/yyyy");
						date = formatter.parse(str);
						return date;
						
					}
						

				}catch(Exception e){
						dfltYear =0;
						System.out.println("Och");
				}
				
				
//				System.out.println(dfltYear);
				if(dfltYear!=0){
//					System.out.println("B4 dflt Year:"+ dateString);
					String dateStringMonthDate=dateString.substring(0, matcher.start());
					try{
//						dateStringMonthDate = GoogleTranslate.translate(dateStringMonthDate);
						dateStringMonthDate = MicrosoftTranslate.translate(dateStringMonthDate);
					}catch(Exception e ){
						date = new Date(01, 01, dfltYear);
						return date;
					}
					pattern = Pattern.compile("jan|feb|mar|apr|may|jun|jul|aug|sep|oct|nov|dec");
					matcher = pattern.matcher(dateStringMonthDate.toLowerCase());
					if(!matcher.find()){
						String str;
//						System.out.println("In No Month Match:"+ dfltYear);
						SimpleDateFormat formatter = new SimpleDateFormat("dd/MM/yyyy");
						pattern = Pattern.compile("\\d{4}");
						matcher = pattern.matcher(dateStringMonthDate.toLowerCase());
						if(matcher.find()){
							dfltMonth = matcher.group().substring(0,1);
							 str = "01/"+dfltMonth+dfltYear;
						}else
							 str = "01/"+"01/"+dfltYear;
						try{
							date = formatter.parse(str);
						}
						catch(Exception e){
							date = new Date(dfltYear,01,01);
						}
						return date;
					}
					else{
						dfltMonth = encodeMonth(matcher.group());
						pattern = Pattern.compile("\\d+");
						matcher = pattern.matcher(dateStringMonthDate);
						matcher.find();
						dfltDay = Integer.parseInt(matcher.group());
//						System.out.println("?------:"+matcher.group());
						String str = dfltDay+"/"+dfltMonth+"/"+dfltYear;
						System.out.println("Final str"+str);
						SimpleDateFormat formatter = new SimpleDateFormat("dd/MM/yyyy"); //please notice the capital M
						try {
							date = formatter.parse(str);
							System.out.println("Final Date:"+date.toString());
						} catch (ParseException e) {
							// TODO Auto-generated catch block
							date = new Date(2011, 01, 01);
						}
					}
//					dfltMonth = matcher.group();
//					System.out.println("After dflt Year:"+ dateStringMonthDate);
//					System.out.println("month:"+ matcher.group()+"->"+encodeMonth(matcher.group()));
					
					
					
				}
		}
		if(date == null)
		return new Date(01, 01, 2011);
		else 
			return date;
	}
	
static String encodeMonth(String month){
		
		if(month.toLowerCase().contains("dec"))
			return "12";
		if(month.toLowerCase().contains("jan"))
			return "01";
		if(month.toLowerCase().contains("feb"))
			return "02";
		if(month.toLowerCase().contains("apr"))
			return "04";
		if(month.toLowerCase().contains("may"))
			return "05";
		if(month.toLowerCase().contains("jun"))
			return "06";
		if(month.toLowerCase().contains("jul"))
			return "07";
		if(month.toLowerCase().contains("sep"))
			return "09";
		if(month.toLowerCase().contains("oct"))
			return "10";
		if(month.toLowerCase().contains("nov"))
			return "11";
		if(month.toLowerCase().contains("mar"))
			return "03";
		if(month.toLowerCase().contains("aug"))
			return "08";
		else
			return "00";
		
		
		
	}
}
